package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao implements BaseTeacherDao {
    private static Teacher[] teac = new Teacher[5];

    static {
        System.out.println("----------------Loding----------------");
        Teacher tea1 = new Teacher("itheima001", "老黄", "23", "1999-11-11");
        Teacher tea2 = new Teacher("itheima002", "老陈", "24", "2000-11-11");
        teac[0] = tea1;
        teac[1] = tea2;
    }

    public boolean addTeacher(Teacher tea) {
        int index = -1;
        for (int i = 0; i < teac.length; i++) {
            Teacher t = teac[i];
            if (t == null) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return false;
        } else {
            teac[index] = tea;
            return true;
        }
    }

    public Teacher[] findAllTeacher() {
        return teac;
    }

    public void deleteTeacherById(String delId) {
        int index = getIndex(delId);
        teac[index] = null;
    }

    public int getIndex(String delId) {
        int index = -1;
        for (int i = 0; i < teac.length; i++) {
            Teacher t = teac[i];
            if (t != null && t.getId().equals(delId)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(String updateId, Teacher newtea) {
        int index = getIndex(updateId);
        teac[index] = newtea;
    }
}
